#####################
### Load Packages ###
#####################

library(haven)


# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")


SummaryTable <- function(CurrentVariable, VariableName)
{
  CurrentTable <- data.frame(matrix(NA,1,0))
  CurrentTable$VariableName <- VariableName
  CurrentTable$Mean <- round(mean(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$Median <- round(median(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$Min <- round(min(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$Max <- round(max(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$SD <- round(sd(CurrentVariable, na.rm=TRUE),2)
  return(CurrentTable)
}

####
# State Leg
####


####
# Stauffer
####

StaufferData <- read_dta("Data/Formatted Data/Stauffer-StateLeg-RegressionData.dta")


CurrentTable <- SummaryTable(StaufferData$state_responsive, "Responsiveness")
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$state_guess, "Estimate % Women"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$gender_r, "Female"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$ideo7, "Ideology"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$Republican, "Republican"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$Independent, "Independent"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$strong_partisan, "Strong Partisan"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$state_ideo01, "State Gov Liberalism"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$congruence, "Congruence"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$religiosity, "Religiosity"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$coll, "College Degree"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$white, "White"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$age, "Age"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$employed, "Employed"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferData$know_state, "Political Knowledge State"))
StaufferFullTable <- rbind(CurrentTable, SummaryTable(StaufferData$state, "Women in State Leg"))       

####
# Clark-Rogers 
####

ClarkRogersData <- read_dta("Data/Formatted Data/Clark-Rogers-RegressionData.dta")

CurrentTable <- SummaryTable(ClarkRogersData$RepPeopleLikeMe_StateLeg, "Responsiveness")
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Trust_StateLeg_Pre, "Trust in State Legislature"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Belief_WomenStateLeg, "Estimate % Women"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Female, "Female"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Ideology, "Ideology"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Republican, "Republican"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Independent, "Independent"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$StrongPartisan, "Strong Partisan"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$State_Ideology, "State Gov Liberalism"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Congruence, "Congruence"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Religiosity, "Religiosity"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$CollegeDegree, "College Degree"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$White, "White"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Age, "Age"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Employed, "Employed"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$StateKnowledge, "Political Knowledge State"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$True_WomenStateLeg, "Women in State Leg"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$RR_Tesler, "Racial Resentment (Tesler)"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$RR_Kinder, "Racial Resentment (Kinder & Sanders)"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Fem_Tesler, "Sexism"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Belief_BlackStateLeg, "Estimate % Black"))  
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$True_BlackStateLeg, "Black in State Leg")) 
RogersTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Black, "Black"))  
RogersTable$Order <- seq(1,nrow(RogersTable))

CombinedTable <- merge(RogersTable, StaufferFullTable, by.x="VariableName", by.y="VariableName", all.x=TRUE)

CombinedTable <- CombinedTable[order(CombinedTable$Order),]
CombinedTable$Order <- NULL

colnames(CombinedTable) <- c("Variable", "Mean", "Median", "Min", "Max", "SD", "Mean", "Median", "Min", "Max", "SD")

write.csv(CombinedTable, "Tables/Appendix-Table-A-7.csv", row.names=FALSE)
